<?php

namespace App\Models\Base;

/**
 * Created by PhpStorm.
 * User: Mountain gao
 * 对个人账户积分的修改，公共类
 */

use Carbon\Carbon;
use Log;


use Illuminate\Support\Facades\DB;


class PointAccount
{
  private $user;
  private $today;
  private $pointSeting;

  public function __construct(Member $member)
  {
    $this->user = $member;

    $this->today = Carbon::today()->toDateString();
//        Carbon::today()->toDateTimeString();
    $this->pointSeting = PointRule::all('action', 'point')->pluck('point', 'action');
  }

  //注册送积分（增加固定积分）
  public function userReg()
  {
    $uid = $this->user->id;
    if ($uid) {
      Log::info(json_encode($this->pointSeting));
      //增加日志
      if ($this->pointSeting['reg'] <> 0) {
        $rechargeArr = array(
          'point' => $this->pointSeting['reg'],
          'type' => 1,
          'obj_id' => $uid,
          'remark' => '新用户注册'
        );
//            //更改积分
        $this->changeAccount($this->user, $rechargeArr);
      } else {
        Log::info($this->pointSeting['1']);
        return true;
      }
      return true;
    } else {
      return false;
    }
  }

  //用户增加客户
  public function userAddCustomer(Customer $customer)
  {
    $uid = $this->user->id;
    if ($uid) {
      //增加流水
      if ($this->pointSeting['addcustomer'] <> 0) {
        $rechargeArr = array(
          'point' => $this->pointSeting['addcustomer'],
          'type' => 1,
          'obj_id' => $customer->id,
          'remark' =>' 新增客户 |' . $customer->id
        );
//            //更改余额
        $this->changeAccount($this->user, $rechargeArr);
      }

      /*
       * 按月奖励 ,从1号到现在
       *
       */
      $startOfMonth = Carbon::now()->startOfMonth();
      $monthCount = Customer::where([['member_id', $uid],['created_at','>',$startOfMonth]])->count();
      Log::info($monthCount.'==monthCount');
//      $monthCount = 30;
      if ($monthCount == 30) {
        $rechargeArr = array(
          'point' => $this->pointSeting['monthcount'],
          'type' => 1,
          'obj_id' => $customer->id,
          'remark' => '当月成功上传客户信息达30次，额外赠送100积分 |' . $customer->id
        );
        $this->changeAccount($this->user, $rechargeArr);
      }
      /*
       * 按总数奖励
       */
      $totalCount = Customer::where([['member_id', $uid]])->count();
      Log::info($totalCount." == totalCount");
//      $totalCount = 100;
      if ($totalCount == 50) {
        $rechargeArr = array(
          'point' => $this->pointSeting['totalcount50'],
          'type' => 1,
          'obj_id' => $customer->id,
          'remark' => '成功上传达50次，额外赠送200积分 |' . $customer->id
        );
        $this->changeAccount($this->user, $rechargeArr);
      }
      elseif ($totalCount == 100) {
        $rechargeArr = array(
          'point' => $this->pointSeting['totalcount100'],
          'type' => 1,
          'obj_id' => $customer->id,
          'remark' =>  '成功上传达100次，额外赠送500积分 |' . $customer->id
        );
        $this->changeAccount($this->user, $rechargeArr);
      }
      return true;

    } else {
      return false;
    }
  }

  //用户使用积分兑换 （减积分）
  public function pointOrderAccountPay(PointOrder $pointOrder)
  {
    Log::info($pointOrder);
    if ($pointOrder) {
      Log::info($this->user->point);
      Log::info($this->user->point - $pointOrder->point);
      //检查余额是否足够支付 account_amount
      if ($this->user->point - $pointOrder->point < 0) {
        return false;
      }
      if ($pointOrder->point > 0) {
        $rechargeArr = array(
          'point' => $pointOrder->point,
          'type' => 2,
          'obj_id' => $pointOrder->id,
          'remark' => '积分兑换商品' . $pointOrder->id
        );
        Log::info(json_encode($rechargeArr));
        return $this->changeAccount($this->user, $rechargeArr);
      }
    } else {
      return false;
    }
  }


  // $objArr 含有type
  protected function changeAccount(Member $member, $objArr)
  {
    Log::info($member);
    Log::info(json_encode($objArr));
    //先加账户，再加流水
    return DB::transaction(function () use ($member, $objArr) {
      if ($objArr['type'] == 1 ) //积分增加
      {
        $member->point = $member->point + $objArr['point']; //积分
        $member->total_point = $member->total_point + $objArr['point']; //总积分
        $member->save();

        $journal = new PointLog();
        $journal->member_id = $member->id;
        $journal->points = $member->point;
        $journal->change = $objArr['point']; //
        $journal->from = $objArr['remark']; //
        $journal->type = 1;
        $journal->save();
        return $journal->id;
      } elseif ($objArr['type'] == 2) //用户兑换
      {
        $account = $member->account;
        $member->point = $member->point - $objArr['point']; //积分
        $member->save();

        $journal = new PointLog();
        $journal->member_id = $member->id;
        $journal->points = $member->point;
        $journal->change = $objArr['point']; //
        $journal->type = $objArr['type'];
        $journal->from = $objArr['remark'];
        $journal->save();
        return $journal->id;
      }

    });

  }

}